import * as THREE from "../../../modules/three/three.module.js";
import {toFixedVector} from "../lib/util/StaticMethods.js";
import SphericalUpZ from "../lib/util/SphericalUpZ.js";
import {r2d, d2r} from "../lib/CommonConsts.js";

const _spherical = new SphericalUpZ()
const _v = new THREE.Vector3()

export default class DirPointVO {
  position = new THREE.Vector3()
  positionGrid = new THREE.Vector3()
  _normal = new THREE.Vector3(0, 1, 0)
  // 以度为单位
  phi = 0
  // 以度为单位
  theta = 0

  setNormal(normal) {
    this._normal.copy(normal)
    _spherical.setFromVector3(normal)
    this.phi = _spherical.phi * r2d
    this.theta = _spherical.theta * r2d
    this.positionGrid.copy(this._normal).multiplyScalar(0.3).add(this.position)
  }
  getMidPos() {
    return _v.copy(this._normal).multiplyScalar(0.5).add(this.position)
  }
  getAbovePos() {
    return this.positionGrid.copy(this.position).add(this._normal)
  }
  toJsonObj(distArr) {
    const arr = []
    const pos = this.position
    arr[0] = parseFloat(pos.x.toFixed(3))
    arr[1] = parseFloat(pos.y.toFixed(3))
    arr[2] = parseFloat(pos.z.toFixed(3))
    arr[3] = parseFloat(this.phi.toFixed(2))
    arr[4] = parseFloat(this.theta.toFixed(2))
    if (distArr) {
      distArr.push(...arr)
    }
    return arr
  }
  fromJsonObj(jsonObj) {
    const [x, y, z, phi, theta] = jsonObj
    this.position.set(x, y, z)
    this.phi = phi
    this.theta = theta
    _spherical.set(1, phi * d2r, theta * d2r)
    _spherical.toVector3(this._normal)
    this.positionGrid.copy(this._normal).multiplyScalar(0.3).add(this.position)
    return this
  }
}
